//
//  yezinghuiSeriesViewController.m
//  LeaClothing
//
//  Created by LeaClothing on 2019/8/2.
//  Copyright © 2019年 LeaClothing. All rights reserved.
//

#import "yezinghuiSeriesViewController.h"
#import "jibeisuSeriesTableViewCell.h"
#import "luodaiClothingTypeViewController.h"

@interface yezinghuiSeriesViewController ()

@property (weak, nonatomic) IBOutlet UINavigationItem *seriesViewNaviItem;
@property (weak, nonatomic) IBOutlet UITableView *clothingSeriesTableView;
@property (nonatomic, strong) NSMutableArray *clothingSeriesArrayDS;

@end

@implementation yezinghuiSeriesViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    ClothingInfoModel*clothingModel = (ClothingInfoModel*)self.clothingSeriesModel;
    _seriesViewNaviItem.title = kFormat(@"%@ series", clothingModel.clothingTypeName);
    _clothingSeriesArrayDS = !clothingModel.clothingSeriesArray?[NSMutableArray arrayWithCapacity:0]:[NSMutableArray arrayWithArray:clothingModel.clothingSeriesArray];
    [_clothingSeriesTableView reloadData];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _clothingSeriesArrayDS.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString * cellID = @"ClothingSeriesCellid";
    jibeisuSeriesTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[jibeisuSeriesTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    cell.seriesCellModel = [_clothingSeriesArrayDS objectAtIndex:indexPath.row];
    cell.seriesCellRowLab.text = kFormat(@"%02ld", indexPath.row+1);
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 80;
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"clothingSeriesViewAddPushid"]) {
        luodaiClothingTypeViewController *addSeriesView = segue.destinationViewController;
        addSeriesView.clothingTypeName = _seriesViewNaviItem.title;
        addSeriesView.type = AddClothingType_series;
        addSeriesView.addClothingSeriesSureBlock = ^(ClothingSeriesModel * _Nonnull seriesModel) {
            [self->_clothingSeriesArrayDS addObject:seriesModel];
            [self->_clothingSeriesTableView reloadData];
            
            ClothingInfoModel*clothingModel = (ClothingInfoModel*)self->_clothingSeriesModel;
            clothingModel.clothingSeriesArray = [NSArray arrayWithArray:self.clothingSeriesArrayDS];
            clothingModel.bg_tableName = clothing_tablename;
            self->_clothingSeriesModel = clothingModel;
            
            dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                NSString * where = [NSString stringWithFormat:@"where %@=%@",bg_sqlKey(bg_createTimeKey),bg_sqlValue(clothingModel.bg_createTime)];
                [clothingModel bg_updateWhere:where];
                
                dispatch_async(dispatch_get_main_queue(), ^{
                    [[NSNotificationCenter defaultCenter] postNotificationName:ClothingWarehouseViewRefresh object:nil];
                });
            });
      
        };
    }
}
- (IBAction)addSeriesViewAddClothingBtnAction:(id)sender {
    [self performSegueWithIdentifier:@"clothingSeriesViewAddPushid" sender:nil];
}

@end
